<?php
 
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Hash; 
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class TrainingController extends Controller
{
    //===============显示列表=============
    function getAdd()
    {
        return view('Personnel/Training/add');
    }

    // 执行添加
    function postInsert(Request $request)
    {
        // dd($request->all());
        $data = $request->only('Title', 'Route', 'PersonID');
        $data['Time'] = time();
        // 文件上传
        $data['Route'] = self::upload($request);
        // dd($data);
        $res = DB::table('Training')->insert($data);
         if($res){
            //成功跳转至人员列表页
            return redirect('Personnel/Training/index')->with('success','添加成功');
         }else{
            return back() ->with('error','添加失败');
         }
    }

    // 显示
    function getIndex(Request $request)
    {
        if($request['title']){

            $res = DB::table('Training')
            ->join('Person', 'person.id', '=', 'Training.PersonID')
            ->where('Training.Title','like','%'.$request['title'].'%')

            ->select('Training.*','PersonName')
            ->paginate(1);
       // dd($res);
        }else{
            $res = DB::table('Training')
            ->join('Person', 'person.id', '=', 'Training.PersonID')
            ->select('Training.*','PersonName')
            ->paginate(1);
        }
        // dd(2222);
        $info = $request->all();
        return view('Personnel/Training/index', ['res'=>$res,'info'=>$info]);
    }

    // 文件下载

    public function getUpload(Request $request){

       
        $text = $request->all()['text'];
        // dd($text);
        return response()->download('C:/Users/Administrator/Desktop/'.$text, $text);
    }

    static function upload(Request $request)
    {
        // 判断是否有文件上传
        if($request->hasFile('Route')){
            if ($request->file('Route')->isValid()) {
                $ext = $request->file('Route')->getClientOriginalExtension();
                $filename = time().'.'.$ext;
                // dd($filename);
                $file = $request->file('Route')->move('./uploads/', $filename);
                return $filename;
            }
        }
    }


    //=============================删除===========================
    public function getDel($id){
        //执行删除
        //dd($id);
        DB::table('Training')->where('TrainingID',$id)->delete();
        //解析模板显示列表的页面
        return redirect('Personnel/Training/index')->with('删除成功');
    }
}
?>